package zzz.jsondemo;




import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.omg.CORBA.Object;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 隼 on 2017/5/16.
 */
public class json1 {


    public static void main(String[] args) {
        createJsonByBean();
    }

    private static void jsonobject(){

        JSONObject wangxiaoer=new JSONObject();
        Object nullobj=null;
        try {
            wangxiaoer.put("name","王小二");
            wangxiaoer.put("age",25.2);
            wangxiaoer.put("birthday","1990-01-01");
            wangxiaoer.put("major",new String[] {"理发","挖掘机"});
            wangxiaoer.put("has_girlfriend",false);
            wangxiaoer.put("car",nullobj);
            wangxiaoer.put("house",nullobj);
            System.out.println(wangxiaoer.toString());
        }catch (JSONException e){
            e.printStackTrace();
        }
    }


//    使用hashMap构造json
    private  static void createJsonByMap(){
       Map  wangxiaoer=new HashMap<String, Object>();
        Object nullobj=null;
        wangxiaoer.put("name","王小二");
        wangxiaoer.put("age",25.2);
        wangxiaoer.put("birthday","1990-01-01");
        wangxiaoer.put("major",new String[] {"理发","挖掘机"});
        wangxiaoer.put("has_girlfriend",false);
        wangxiaoer.put("car",nullobj);
        wangxiaoer.put("house",nullobj);
        System.out.println(new JSONObject().fromObject(wangxiaoer).toString());
    }

    //使用Javabean创建对象----????????????????????????????
    private static void createJsonByBean(){
        Diaosi test=new Diaosi();
        test.setName("王小二");
        test.setAge(25.2);
        test.setBirthday("1990-01-01");
        test.setHas_girlfriend(false);
        test.setCar(null);
        test.setMajor(new String[] {"理发","挖掘机"});
        test.setHouse(null);

        System.out.println(new JSONObject().fromObject(test).toString());




    }

}

